package cn.bdqn.t354.service.impl;

import cn.bdqn.t354.mapper.CustomerMapper;
import cn.bdqn.t354.pojo.Customer;
import cn.bdqn.t354.pojo.User;
import cn.bdqn.t354.service.CustomerService;
import cn.bdqn.t354.vo.ConsRptVo;
import cn.bdqn.t354.vo.ContrRptVo;
import cn.bdqn.t354.vo.SvrRptVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CustomerServiceImpl implements CustomerService {
    @Resource
    private CustomerMapper customerMapper;

    @Override
    public void addCustomer(Customer customer) {
        customerMapper.insert(customer);
    }

    @Override
    public IPage<Customer> getCustomersByPage(String custName, String custNo,
                            String region, String managerName,
                            String level, Integer pageNo, Integer pageSize) {
        //组装分页条件
        Page<Customer> page=new Page<>(pageNo,pageSize);
        //组装过滤条件 动态sql
        QueryWrapper<Customer> qw=new QueryWrapper();
        qw.like(custName!=null&&!"".equals(custName), "cust_name",custName);
        qw.eq(custNo!=null&&!"".equals(custNo),"cust_no",custNo);
        qw.eq(region!=null&&!"".equals(region),"cust_region",region);
        qw.like(managerName!=null&&!"".equals(managerName),"cust_manager_name",managerName);
        qw.like(level!=null&&!"".equals(level),"cust_level",level);

        //客户经理只查自己创建的客户数据
        User user=(User)SecurityUtils.getSubject().getPrincipal();
        if(user.getRole().getRoleId()==2){//客户经理
            qw.eq("cust_manager_id",user.getUsrId());
        }

        //调用分页方法
        IPage<Customer> customerPage=customerMapper.selectPage(page, qw);
        //返回分页对象
        return customerPage;
    }

    @Override
    public Customer getCustomer(String custNo) {
        return customerMapper.selectById(custNo);
    }

    @Override
    public void updateCustomer(Customer customer) {
        customerMapper.updateById(customer);
    }

    @Override
    public int deleteCustomer(String custNo) {
        //1.删除相关联系人

        //2.删除相关订单
        //3.删除相关交往记录
        //4.删除客户
        return customerMapper.deleteById(custNo);
    }

    @Override
    public List<Customer> findAllCustomers() {
        return customerMapper.selectList(null);
    }
}
